package net.entelijan.cobean.bind;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public abstract class AbstractPropertyBinding<C> implements IBinding<C> {

    private static Log log = LogFactory.getLog(AbstractPropertyBinding.class);

    private static final long serialVersionUID = 1L;

    protected String modelProperty;

    protected IPropertyBindingStrategy propertyBindingStrategy;

    public AbstractPropertyBinding() {
        super();
        this.propertyBindingStrategy = BindingStrategyFactory.createBindingStrategy();
    }

    public <T> T bind(T model, C comp) {
        validateBindingConfiguration();
        log.debug("[" + getClass().getName() + "#bind] model=" + model + " comp=" + comp);
        return this.propertyBindingStrategy.bind(model, this, comp);
    }

    public String getModelProperty() {
        return this.modelProperty;
    }

    public void setModelProperty(String modelProperty) {
        this.modelProperty = modelProperty;
    }

    public IPropertyBindingStrategy getPropertyBindingStrategy() {
        return this.propertyBindingStrategy;
    }

    public void setPropertyBindingStrategy(IPropertyBindingStrategy propertyBindingStrategy) {
        this.propertyBindingStrategy = propertyBindingStrategy;
    }

    protected String convertToString(Object value) {
        String re = "";
        if (value != null) {
            re = value.toString();
        }
        return re;
    }

    /**
     * Validates the configuration of the binding befor the actual bindin takes place.
     */
    protected void validateBindingConfiguration() {
        // No validation on default
    }


}
